
#include "client_request.h"


int do_client(int cfd, sqlite3 *pDb)						//与客户端数据收发
{
    char buf[512] = {0};
	MSG msg;
	memset(&msg,0,sizeof(MSG));
	while(recv(cfd, (char*)&msg, sizeof(MSG), 0) > 0)
	{	   
	   printf("%d %s %s\n",msg.type,msg.data,msg.name);
	   switch(msg.type)
	   {
	  	 case R:
			do_register(cfd, &msg, pDb);					//用户注册，把用户名和密码插入到表usr中：(name text primary key, password text)
			 break;
		 case L:
			do_login(cfd, &msg, pDb);						//用户登录，把用户输入的用户名和密码在usr表中进行查询，找到匹配项，提醒用户登录成功；
			 break;
		 case S:
			 do_set(cfd, &msg, pDb);						//设置温湿度
			 break;
		 case H:
			 do_history(cfd, &msg, pDb);					//历史记录查询
			 break;
		 case A:							
			do_authorize(cfd, &msg, pDb);					//添加授权	
			break;
		 case D:
			do_delete(cfd, &msg, pDb);						//撤销授权或者删除用户		
			break;
		 default:
			break;
	   }
	   memset(&msg,0,sizeof(MSG));
	}
	return 0;
}

int do_register(int cfd, MSG *msg, sqlite3 *pDb)	
{
	char * errmsg;
	char **presult;
	char Sql[128];
	int row, column, index;
	printf("in do register\n");	
	sprintf(Sql, "select * from Usr_Info_Table where name = '%s';", msg->name);
	if(GetTable(Sql, &presult, &row, &column, pDb) == SQ_OK)
	{	
		printf("****\n");
		
		if(row!=0)
		{
		printf("------\n");
		memset(msg,0,sizeof(MSG));
		strcpy(msg->data, "registerError");
		}
		else
		{
			printf("++++++\n");
			sprintf(Sql, "insert into Usr_Info_Table (name,password) values('%s', '%s');", msg->name,msg->data);
			printf("%s\n",Sql);
			if(ExecSQL(Sql, pDb) == SQ_OK)
			{
				printf("insert card ok\n");
				memset(msg,0,sizeof(MSG));
				strcpy(msg->data, "registerOk");
			}
			else
			{
				memset(msg,0,sizeof(MSG));
				strcpy(msg->data, "register error");
				
			}
			
		}
	}
	if(send(cfd, msg, 128,0) < 0)
		{
			perror("send");
			memset(msg,0,sizeof(MSG));
			return -1;
		}
		memset(msg,0,sizeof(MSG));
		return 0;
	
}


int do_login(int cfd, MSG *msg, sqlite3 *pDb)
{
	char Sql[128];
	char **presult;
	int row = 0, column = 0;
	sprintf(Sql, "select * from Usr_Info_Table where name = '%s' and password = '%s';",msg->name, msg->data);
	printf("%s\n",Sql);
	if(GetTable(Sql, &presult, &row, &column, pDb) != SQ_OK)
	{
		printf("select usr error\n");
		return -1;
	}
	memset(msg,0,sizeof(MSG));
	if(row == 0)
	{
		printf("no such a usr\n");
		strcpy(msg->data, "loginPswError");
	}
	else if(row == 1)
	{

		printf("there have a right usr\n");
		strcpy(msg->data, "loginOk");
	}
	if(send(cfd, msg, 128, 0) < 0)
	{
		perror("send");
		return -1;
	}
	printf("msg->data = %s\n",msg->data);
	return 0;
}
int do_set(int cfd, MSG *msg, sqlite3 *pDb)
{
	char* buf;
	buf = msg->data;
	printf("from cli up and down: %s",buf);
	//split string
	buf[strlen(buf)] = '\0';
	int i = 0, argc = 0;
	char* argv[100] = {NULL};
	char *tmp = strtok(buf, ",");
	while(tmp)
	{		
		argv[i++] = tmp;
		tmp = strtok(NULL, ",");
	}
	 g_Max_Tem	= atof(argv[0]);
	 g_Min_Tem	= atof(argv[1]);
	 g_Max_Hum = atof(argv[2]);
	 g_Min_Hum = atof(argv[3]);
	 printf("max_tem=%f,max_hum=%f,min_tem=%f,min_hum=%f\n",g_Max_Tem,g_Min_Tem,g_Max_Hum,g_Min_Hum);
	 memset(msg,0,sizeof(MSG));
	 memcpy(msg->data,"set hum and tem limit Ok!",512);
	 if(send(cfd, msg ,sizeof(MSG), 0) < 0)
	{
		perror("send");
		return -1;
	}

}
int do_history(int cfd, MSG *msg, sqlite3 *pDb)				//历史记录查询		2018-6-29 19:30*2018-6-29 20:30
{
	char Sql[128];
	char date1[64] = {0};
	char date2[64] = {0};
	char **presult;
	char *buff ;
	int row, column, index, j, i, x, y, z;
    int s = 0;
	buff = msg->data;
	/*while(*p != '*')
	{
		date1[i++] = *p;
		p++;
	}
	p++;*/
	buff[strlen(buff)] = '\0';
	int k = 0, argc = 0;
	char* argv[100] = {NULL};
	char *tmp = strtok(buff, "*");
	while(tmp)
	{		
		argv[k++] = tmp;
		tmp = strtok(NULL, "*");
	}
	strncpy(date1, argv[0],strlen(argv[0]) + 1);
	strncpy(date2, argv[1],strlen(argv[1]) + 1);
	
	sprintf(Sql, "select * from Env_Info_Table where date > '%s' and date < '%s' ;",date1, date2);
	
	if(GetTable(Sql, &presult, &row, &column, pDb) != SQ_OK)
	{
		printf("select history error\n");
		return -1;
	}
	
	// 发送查询结果
	char buf[512]={0};
	memset(Sql, 0, sizeof(Sql));
	index = column;
	for(i = 0; i < row; i++)
	{
		x = index++;
		y = index++;
		z = index++;
		j = 0;
		sprintf(Sql, "%s = %s %s = %s %s = %s\n", presult[j], presult[x], presult[j+1], presult[y], presult[j+2], presult[z]);
		//printf("%s",Sql);
		memset(msg, 0, sizeof(MSG));
		strncpy(msg->data, Sql, strlen(Sql));
		printf("%s",msg->data);
		msg->type = H;
		if(send(cfd, msg, sizeof(MSG), 0) < 0)
		{
			perror("send");
			return -1;
		}
		sleep(1);
        s++;
        printf("%d\n",s);
	}
	return 0;
}

int do_authorize(int cfd, MSG *msg, sqlite3 *pDb)			//授权卡
{
	char * errmsg;
	char Sql[128] = {0};
	sprintf(Sql, "insert into Card_Info_Table values ('%s', '%s');", msg->data, msg->name);
	printf("%s\n", Sql);
	memset(msg,0,sizeof(MSG));
	if(ExecSQL(Sql, pDb) != SQ_OK)
	{
		printf("insert card error\n");
		strcpy(msg->data, "card number already exist");
	}
	else
	{
		printf("do authorization ok\n");
		strcpy(msg->data, "do authorization ok");
	}
    if(send(cfd, msg ,sizeof(MSG), 0) < 0)
	{
		perror("send");
		return -1;
	}
	return 0;
}


int do_delete(int cfd , MSG *msg, sqlite3 *pDb)			//删除用户和取消授权
{
	char * errmsg;
	char **presult;
	char Sql[128];
	int row, column, index;
	printf("in do delete\n");	
	if(!strncmp(msg->name, "card", 4))						//注销卡	card
	{
		
			sprintf(Sql, "select * from Card_Info_Table where card_no = '%s';", msg->data);
			if(GetTable(Sql, &presult, &row, &column, pDb) == SQ_OK)
			{	
				printf("****\n");
				
				if(row==0)
				{
				printf("------\n");
				memset(msg,0,sizeof(MSG));
				strcpy(msg->data, " The username not exist");
				}
				else
				{
					printf("++++++\n");
					sprintf(Sql, "delete from Card_Info_Table where card_no = '%s';", msg->data);
					printf("%s\n",Sql);
					if(ExecSQL(Sql, pDb) == SQ_OK)
					{
						printf("delete card ok\n");
						memset(msg,0,sizeof(MSG));
						strcpy(msg->data, "delete card ok");
					}
					
				}
			}

		if(send(cfd, msg, sizeof(MSG),0) < 0)
		{
			perror("send");
			memset(msg,0,sizeof(MSG));
			return -1;
		}
		memset(msg,0,sizeof(MSG));
		return 0;
	}
	else																	//删除用户
	{
		//memset(presult,0,sizeof(presult));
		//memset(Sql,0,sizeof(Sql));		
		sprintf(Sql, "select * from Usr_Info_Table where name = '%s' and password = '%s';",msg->name, msg->data);
		if(GetTable(Sql, &presult, &row, &column, pDb) == SQ_OK)
		{	
			printf("****\n");
			
			if(row==0)
			{
			printf("------\n");
			memset(msg,0,sizeof(MSG));
			strcpy(msg->data, " your input username or password error");
			}
			else
			{
				printf("**************\n");
				sprintf(Sql, "delete from Usr_Info_Table where name = '%s' and password = '%s';",msg->name, msg->data);
				printf("%s\n",Sql);
				if(ExecSQL(Sql, pDb) != SQ_OK)
				{
					printf("delete usr error\n");
				}				
				else
				{
					printf("delete ok \n");
					memset(msg,0,sizeof(MSG));
					strcpy(msg->data, "delete usr ok");
				}
			}
		}
		if(send(cfd, msg, sizeof(MSG), 0) < 0)
		{
			perror("send");
			return -1;
		}
		return 0;
	}
	
}
